package mapEditor.view.assetEdit
{
	import asset.mapEditorAssetPanelAsset;
	import asset.mapEditorAssetPanelToolAsset;
	
	import fl.containers.ScrollPane;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FileListEvent;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.net.FileFilter;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import loader.LoaderBase;
	import loader.LoaderEvent;
	import loader.LoaderFactory;
	import loader.LoaderFlagType;
	import loader.LoaderQueue;
	import loader.LoaderType;
	import loader.URLLoaderBase;
	
	import mapEditor.MapEditorManager;
	import mapEditor.data.AssetTemplateInfo;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.vo.AssetGroupVO;
	import mapEditor.vo.AssetVO;
	
	import stages.StageReflection;
	
	import utils.DisplayUtil;
	import utils.StringUtil;
	
	public class AssetPanelView extends mapEditorAssetPanelAsset
	{
		private var _mapEditorAssetPanelToolAsset:mapEditorAssetPanelToolAsset;
		private var _newAssetloaderQueue:LoaderQueue;
		private var _scrollPane:ScrollPane;
		private var _editAssetItemView:AssetItemView;
		private var _selectAssetItemView:AssetItemView;
		private var _assetListView:AssetListView;
		
		public function AssetPanelView()
		{
			_mapEditorAssetPanelToolAsset=new mapEditorAssetPanelToolAsset();
			addChild(_mapEditorAssetPanelToolAsset);
			setView();
			addEvent();
		}
		
		private function setView():void
		{
			_assetListView=new AssetListView();
			_assetListView.addEventListener(Event.CHANGE, __onAssetListViewChange);
			_assetListView.addEventListener(Event.SELECT, __onAssetListItemSelect);
			_scrollPane=new ScrollPane();
			_scrollPane.width=160;
			_scrollPane.source=_assetListView;
			posPanel.addChild(_scrollPane);
			resetView();
			_mapEditorAssetPanelToolAsset.btnAdd.buttonMode=_mapEditorAssetPanelToolAsset.btnEdit.buttonMode=true;
		}
		
		private function addEvent():void
		{
			StageReflection.stage.addEventListener(Event.RESIZE, __onStageResize);
			_mapEditorAssetPanelToolAsset.btnAdd.addEventListener(MouseEvent.CLICK, __onBtnAddClick);
			_mapEditorAssetPanelToolAsset.btnEdit.addEventListener(MouseEvent.CLICK, __onBtnEditClick);			
		}
		
		private function removeEvent():void
		{
			StageReflection.stage.removeEventListener(Event.RESIZE, __onStageResize);
			_mapEditorAssetPanelToolAsset.btnAdd.removeEventListener(MouseEvent.CLICK, __onBtnAddClick);
			_mapEditorAssetPanelToolAsset.btnEdit.removeEventListener(MouseEvent.CLICK, __onBtnEditClick);
			if(_newAssetloaderQueue)
			{
				_newAssetloaderQueue.removeEventListener(LoaderEvent.COMPLETE_QUEUE, __onAssetLoadComplete);
				_newAssetloaderQueue.dispose();
				_newAssetloaderQueue=null;
			}
		}
		
		private function setScrollPane():void
		{
			_scrollPane.height=StageReflection.stageHeight-_mapEditorAssetPanelToolAsset.height-90-20-2-20;
		}
		
		private function __onBtnEditClick(evt:MouseEvent):void
		{
			MapEditorManager.instance.selectAssetItem(_selectAssetItemView.assetVO);
		}
		
		private function __onBtnAddClick(evt:MouseEvent):void
		{
			var file:File = File.applicationDirectory;
			var openFileFilter:FileFilter = new FileFilter("SWF", "*.swf");
			file.browseForOpenMultiple("请选择资源文件", [openFileFilter]);
			file.addEventListener(FileListEvent.SELECT_MULTIPLE, __onOpenSelectHandle);
		}
		
		/**
		 *打开文件选择完成后 
		 */		
		private function __onOpenSelectHandle(evt:FileListEvent):void
		{
			var files:Array = evt.files;
			_newAssetloaderQueue=new LoaderQueue();
			for each(var file:File in files)
			{
				var load:LoaderBase=LoaderFactory.Instance.createLoader(LoaderType.LOADER, file.nativePath, LoaderFlagType.COMMON, false, false);
				load.addEventListener(LoaderEvent.COMPLETE, __onAssetLoadComplete);
				_newAssetloaderQueue.addLoader(load);
			}
			_newAssetloaderQueue.execute();
		}
		
		private function __onAssetLoadComplete(evt:LoaderEvent):void
		{
			evt.baseLoader.removeEventListener(LoaderEvent.COMPLETE, __onAssetLoadComplete);
			var fileNameArr:Array=evt.baseLoader.url.split('\\');
			var fileName:String=fileNameArr[fileNameArr.length-1];
			
			var bytes:ByteArray=evt.baseLoader.data;
			bytes.endian=Endian.LITTLE_ENDIAN;
			bytes.writeBytes(bytes,8);
			bytes.uncompress();
			bytes.position=Math.ceil(((bytes[0]>>>3)*4+5)/8)+4;
			while(bytes.bytesAvailable>2)
			{
				var head:int=bytes.readUnsignedShort();
				var size:int=head&63;
				if (size==63)size=bytes.readInt();
				if (head>>6!=76)
				{
					bytes.position+=size;
				}
				else
				{
					head=bytes.readShort();
					for(var i:int=0;i<head;i++)
					{
						bytes.readShort();
						size=bytes.position;
						while(bytes.readByte()!=0){};
						size=bytes.position-(bytes.position=size);
						var assetVO:AssetVO=new AssetVO();
						assetVO.className=bytes.readUTFBytes(size);
						AssetTemplateInfo.instance.addAsset("asset/"+fileName, assetVO);
					}
				}
			}
			AssetTemplateInfo.instance.assetGroupChange();
			evt.baseLoader.dispose();
			evt.baseLoader=null;
		}
		
		private function __onAssetItemViewSelect(evt:MapEditorEvent):void
		{
			if(_selectAssetItemView)_selectAssetItemView.isSelected=false;
			_selectAssetItemView=evt.currentTarget as AssetItemView;
		}
		
		private function __onAssetItemViewEdit(evt:MapEditorEvent):void
		{
			if(_editAssetItemView)_editAssetItemView.isEdit=false;
			_editAssetItemView=evt.target as AssetItemView;
			MapEditorManager.instance.selectAssetItem(_editAssetItemView.assetVO);
		}
		
		private function __onStageResize(evt:Event):void
		{
			resetView();
		}
		
		private function resetView():void
		{
			_mapEditorAssetPanelToolAsset.y=StageReflection.stageHeight-_mapEditorAssetPanelToolAsset.height-90-20;
			setScrollPane();
		}
		
		private function __onAssetListViewChange(evt:Event):void
		{
			_scrollPane.refreshPane();
			_scrollPane.update();
		}
		
		private function __onAssetListItemSelect(evt:Event):void
		{
			_selectAssetItemView=_assetListView.selectAssetItemView;
		}
		
		public function dispose():void
		{
			removeEvent();
			DisplayUtil.dispose(_mapEditorAssetPanelToolAsset);
			_mapEditorAssetPanelToolAsset=null;	
			if(_assetListView)
			{
				_assetListView.removeEventListener(Event.CHANGE, __onAssetListViewChange);
				_assetListView.removeEventListener(Event.SELECT, __onAssetListItemSelect);
				DisplayUtil.dispose(_assetListView);
				_assetListView=null;
			}				
			if(_scrollPane && _scrollPane.parent) _scrollPane.parent.removeChild(_scrollPane);
			_scrollPane=null;
			_editAssetItemView=null;
			_selectAssetItemView=null;
			if(parent)parent.removeChild(this);
		}
	}
}